import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { CrudConfigService } from '@nestjsx/crud';
import { hashSync } from 'bcryptjs';
import { Area } from 'libs/db/entity/area.entity';
import { User } from 'libs/db/entity/user.entity';
import { utilities as nestWinstonModuleUtilities, WinstonModule } from 'nest-winston';
import { nextTick } from 'process';
import { getRepository, Repository } from 'typeorm';
import * as winston from 'winston';
import { Villager } from './../../../libs/db/src/entity/villager.entity';
import { AppModule } from './app.module';
import * as bodyParser from 'body-parser';

CrudConfigService.load({
  query: {
    cache: 2000,
  },
  params: {
    id: {
      field: 'id',
      type: 'number',
      primary: true,
    },
  },
  routes: {
    createOneBase : {
      returnShallow : true
    },
    createManyBase : {
    },
    updateOneBase: {
      allowParamsOverride: true,
      returnShallow: true
    },
    replaceOneBase: {
      allowParamsOverride : true,
      returnShallow : true
    },
    deleteOneBase : {
      returnDeleted : true,
    },
  },
});



async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    logger: WinstonModule.createLogger({
      transports: [
        new winston.transports.Console({
          format: winston.format.combine(
            winston.format.timestamp(),
            nestWinstonModuleUtilities.format.nestLike(),
          ),
        }),
        // other transports...
      ],
      level: "error"
    }),
  })


  app.use(bodyParser.json({limit: '50mb'}));
  app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
  app.setGlobalPrefix('admin-api');
  app.useGlobalPipes(new ValidationPipe());
  app.enableCors()

  const options = new DocumentBuilder()
    .setTitle('Cats example')
    .setDescription('The cats API description')
    .setVersion('1.0')
    .addBearerAuth()
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('/admin-api/api-docs-admin', app, document);


  await app.listen(process.env.ADMIN_PORT, () => {
    // userInit()
    // villagerInit()
    // areaInit()
  });
}
bootstrap();

